# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid

from django.db.utils import IntegrityError
from rest_framework.views import APIView

from components.models import ResourceUser
from components.resource_management.resource_user.dao import query_resource_user, query_resource_user_info
from components.resource_management.resource_user.serializers import ResourceUserSerializer
from utils.mysql_utils import sql_encryption
from utils.views import Result
# from django.core.exceptions import

class ResourceUsersInfo(APIView):
    def get(self, request):
        data = request.query_params.copy()

        resource_dict = query_resource_user_info(data['resource_user_id'])
        if resource_dict['resource_user_default']:
            resource_dict['resource_user_default'] = True
        return Result.success_response(data=resource_dict, msg='查询成功')

    def post(self, request):
        data = request.data

        data['resource_user_code'] = 'service_host'
        data['status'] = 1

        if 'resource_user_id' in data: data.pop('resource_user_id')

        if data['resource_user_default']:
            ResourceUser.objects.filter(resource_user_ids=data['resource_user_ids']).update(resource_user_default=0)
        if data['resource_user_pass']:
            data['resource_user_pass'] = sql_encryption(data['resource_user_pass'])

        try:
            save_data = ResourceUser(**data)
            save_data.save()
        except IntegrityError:
            return Result.fail_response(msg="重复的用户名，请检查")
        except Exception as e:
            return Result.fail_response(msg=str(e))

        return Result.success_response(msg='保存成功')

    def put(self, request):
        data = request.data

        if data['resource_user_default']:
            ResourceUser.objects.filter(resource_user_ids=data['resource_user_ids']).update(resource_user_default=0)
            data['resource_user_default'] = 1
        else:
            data['resource_user_default'] = 0

        if data['resource_user_pass']:
            data['resource_user_pass'] = sql_encryption(data['resource_user_pass'])

        resourceuser_object = ResourceUser.objects.get(resource_user_id=data['resource_user_id'])
        serializer = ResourceUserSerializer(resourceuser_object, data=data)
        # serializer = ResourceUserSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
        else:
            errors = serializer.errors.serializer.default_error_messages['invalid']
            return Result.fail_response(msg=str(errors))

        return Result.success_response(msg='修改成功')

    def delete(self, request):
        data = request.data.copy()

        data['resource_user_id'] = [data['resource_user_id']] if not isinstance(data['resource_user_id'], list) else data[
            'resource_user_id']

        ResourceUser.objects.filter(resource_user_id__in=data['resource_user_id']).update(
            resource_user_ids=uuid.uuid1(), resource_user_name='[删除的用户]', status=0)

        return Result.success_response(msg='删除成功')
